package com.example.demo.service.imp;

import com.example.demo.contrller.data.ReturnData;
import com.example.demo.dao.CircleDao;
import com.example.demo.dao.CircleLikeDao;
import com.example.demo.dao.CommentDao;
import com.example.demo.dao.UserDao;
import com.example.demo.demos.Circle;
import com.example.demo.demos.CircleLike;
import com.example.demo.demos.Comment;
import com.example.demo.demos.User;
import com.example.demo.service.CircleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.demo.utils.TokenUtils;

@Service
public class CircleServiceImp implements CircleService {
    @Resource
    CircleDao circleDao;
    @Autowired
    UserDao userDao;
    @Resource
    CircleLikeDao circleLikeDao;
    @Resource
    CommentDao commentDao;
    @Override
    public ReturnData getList(int page, int pageSize, String token) {
        String username = TokenUtils.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        List<Map<String, Object>> list = circleDao.getList((page - 1) * pageSize, pageSize, user.getId());
        /**
         * 用来查找评论数据
         */
        List returnList = new ArrayList();//用来返回给前端的新列表
        for (int i=0;i<list.size();i++){
            List<Map<String, Object>> comments=
                 commentDao.getList((Integer)list.get(i).get("id"));//查找对应的评论列表
            HashMap map=new HashMap(list.get(i));
            map.put("comments",comments);//存入对象
            returnList.add(map);
        }

        long num = circleDao.count();//获取表里面所有数据的条数
        long total = 0;//页码数
        if (num % pageSize == 0) {
            total = num / pageSize;
        } else {
            total = num / pageSize + 1;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("list", returnList);
        return ReturnData.ok("成功", map);
    }

    @Override

    public ReturnData add(String token, Circle circle) {
        if (!(StringUtils.hasText(circle.getContent()) || StringUtils.hasText(circle.getImages()))) {
            return ReturnData.error("发布内容为空");
        }
        String username = TokenUtils.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        circle.setUser_id(user.getId());
        circle.setTime(new Timestamp(System.currentTimeMillis()));
        circleDao.save(circle);
        return ReturnData.ok("发布成功");
    }

    @Override
    public ReturnData mylist(int page, int pageSize, String token) {
        String username = TokenUtils.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        List<Map<String, Object>> list = circleDao.mylist((page - 1) * pageSize, pageSize, user.getId());
        long num = circleDao.findByUser_id(user.getId()).size();//获取表里面所有数据的条数
        long total = 0;//页码数
        if (num % pageSize == 0) {
            total = num / pageSize;
        } else {
            total = num / pageSize + 1;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("list", list);
        return ReturnData.ok("成功", map);
    }

    @Override
    public ReturnData like(String token, CircleLike circleLike) {
        String username = TokenUtils.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        circleLike.setUserId(user.getId());//设置当前点赞或取消点赞的用户Id
        circleLike.setTime(new Timestamp(System.currentTimeMillis()));//设置操作时间
        List<CircleLike> list = circleLikeDao.findByCircleIdAndUserId(circleLike.getCircleId(), user.getId());
        if (list.size() > 0) { //大于0，表示用户已经操作过这条圈子，执行更新表操作
            circleLike.setId(list.get(0).getId());
        }
        circleLikeDao.save(circleLike);
        if (circleLike.getStatus() == 0) {
            return ReturnData.ok("取消点赞成功");
        } else {
            return ReturnData.ok("点赞成功");
        }

    }

    @Override
    public ReturnData myLikelist(int page, int pageSize, String token) {
        String username = TokenUtils.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        List<Map<String, Object>> list = circleDao.myLikelist((page - 1) * pageSize, pageSize, user.getId());
        long num = 1;//获取表里面所有数据的条数
        long total = circleLikeDao.getCountMyLike(user.getId());//页码数
        if (num % pageSize == 0) {
            total = num / pageSize;
        } else {
            total = num / pageSize + 1;
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("list", list);
        return ReturnData.ok("成功", map);
    }

    @Override
    public ReturnData getLikeUsers(int id) {
        return  ReturnData.ok("成功", circleLikeDao.getLikeUsers(id));
    }

    @Override
    public ReturnData delete(String token, Circle circle) {
        String username = TokenUtils.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        circle =circleDao.findById(circle.getId()).get();

        if (circle.getUser_id()!=user.getId()){
            return ReturnData.error("没有权限删除");
        }
        circleDao.delete(circle);
        return ReturnData.ok("删除成功");
    }

    @Override
    public ReturnData comment(String token, Comment comment) {
       if (!StringUtils.hasText(comment.getContent())){
           return ReturnData.error("请输入评论内容");
       }
        String username = TokenUtils.getUsernameByToken(token);//根据token那用户名
        User user = userDao.findByUsername(username).get(0);//根据用户名查询用户信息
        comment.setUserId(user.getId());//设置评论的用户ID
        comment.setTime(new Timestamp(System.currentTimeMillis()));//设置时间
        commentDao.save(comment);//保存数据
        return ReturnData.ok("评论成功");
    }

    @Override
    public ReturnData getCommentList(int id) {
        return ReturnData.ok("获取成功",commentDao.getList(id));
    }

    @Override
    public ReturnData delComment(String token, Comment comment) {
        String username = TokenUtils.getUsernameByToken(token);;//根据token那用户名
        User user = userDao.findByUsername(username).get(0);//根据用户名查询用户信息

        comment = commentDao.findById(comment.getId()).get();//判断当前用户的id和发布该评论的用户id是否相等

       if (comment.getUserId()==user.getId()){
           commentDao.delete(comment);//删除评论
           return ReturnData.ok("删除成功");
       }else {//没权限
           return ReturnData.error("没有权限删除");
       }
    }
}
